package org.example.leetcpde;

import org.junit.jupiter.api.Test;

public class swapPairs implements IListNode {
    @Test
    void aa() {
        int i = 0;
        for (; i != 0; ) {
            System.out.println(11);
        }
    }


    @Test
    void swapPairs() {
        ListNode node = new ListNode().fill("[1,2,3,4]");
        swapPairsV2(node);
    }

    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        ListNode newHead = head.next;
        head.next = swapPairs(newHead.next);
        newHead.next = head;
        return newHead;
    }

    public ListNode swapPairsV2(ListNode head) {
        ListNode root = new ListNode(0, head);

        for (ListNode tail = root, n1, n2; (n1 = tail.next) != null && (n2 = n1.next) != null; ) {
            n1.next = n2.next;
            n2.next = n1;
            tail.next = n2;
            tail = n1;
        }

        return root.next;
    }
}
